package com.epcs.model.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;

import com.epcs.model.dto.Eventcategory;
import com.epcs.model.dto.Following;
import com.epcs.model.dto.MostFollowingList;
import com.epcs.util.DBUtil;

public class EventCategoryDAO {
    public List<Eventcategory> getAllEventcategories(boolean includeDeactiveItems){
	
	List<Eventcategory> eventcategorylist = new ArrayList<Eventcategory>();
	Session session = DBUtil.getSession();
	try {
	    Criteria criteria = session.createCriteria(Eventcategory.class);
	    if (!includeDeactiveItems){
	    	criteria.add(Restrictions.eq("isActive", true));
	    }
	    criteria.addOrder(Order.desc("eventCategoryId"));	
	    eventcategorylist = criteria.list();
	    if (eventcategorylist.size() > 0)
		return eventcategorylist;
	} finally {

	    if (session != null) {
		session.close();
	    }
	}
	
	return null;
    }
    public Eventcategory getEventCategory(String name){
	
	Eventcategory eventcategory = new Eventcategory();
	
	Session session = DBUtil.getSession();
	try {
	    Criteria criteria = session.createCriteria(Eventcategory.class);
	    criteria.add(Restrictions.eq("eventCategoryName", name));
	    eventcategory = (Eventcategory) criteria.uniqueResult();
	    if (eventcategory!=null )
		
		return eventcategory;
	} finally {

	    if (session != null) {
		session.close();
	    }
	}
	return null;
    }
    public Eventcategory getEventCategoryByID(int id){
	
	Eventcategory eventcategorybyid= new Eventcategory();
	Session session = DBUtil.getSession();
	try {
	    Criteria criteria = session.createCriteria(Eventcategory.class);
	    criteria.add(Restrictions.eq("eventCategoryId", id));
	    eventcategorybyid = (Eventcategory) criteria.uniqueResult();
	    if (eventcategorybyid!=null )
		
		return eventcategorybyid;
	} finally {

	    if (session != null) {
		session.close();
	    }
	}
	return null;
    }
    
    public List<Eventcategory> getEventCategoryListByCategoryIdList(List<Integer> listCategoryId){
    	if(listCategoryId != null){
    		List<Eventcategory> eventcategorylist = new ArrayList<Eventcategory>();
    		if(listCategoryId.size() >0){
    	    	Session session = DBUtil.getSession();
    	    	try {
    	    	    Criteria criteria = session.createCriteria(Eventcategory.class)
    	    	    		.add(Restrictions.in("eventCategoryId", listCategoryId));
    	    	    eventcategorylist = criteria.list();
    	    	    if (eventcategorylist.size() > 0)
    	    		return eventcategorylist;
    	    	} finally {
    	
    	    	    if (session != null) {
    	    		session.close();
    	    	    }
    	    	}
    		}
    		return eventcategorylist;
    	}
    	return null;
        }
    public boolean insertCategoryToDB(Eventcategory category) {
		Session session = null;
		Transaction transaction = null;
		try {
			// get session
			session = DBUtil.getSession();
			// open transaction
			transaction = session.beginTransaction();
			session.save(category);
			transaction.commit();

			return true;
		} catch (HibernateException e) {
			transaction.rollback();
			System.out.println("Rolback: \n" + e.getMessage());

		} finally {
			if (session != null) {
				session.close();
			}
		}
		return false;
	}
	public boolean updateCategory(Eventcategory eventCategory) {
		Session session = DBUtil.getSession();
		Transaction trans = null;
		try {
			trans = session.beginTransaction();
			session.update(eventCategory);
			trans.commit();
			return true;
		} catch (HibernateException e) {
			System.out.println(e.getMessage());
			trans.rollback();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return false;
	}
	public List<MostFollowingList> getAllFollowingCategory() {
		
			List<MostFollowingList> list = new ArrayList<MostFollowingList>();
		
				Session session = null;
				try {
					session = DBUtil.getSession();
					Criteria criteria = session
							.createCriteria(Following.class)

							.setProjection(
									Projections.projectionList()
											.add(Projections.groupProperty("idOfFollow"), "idOfFollow")
											.add(Projections.rowCount(), "countById"))
							.add(Restrictions.eq("typeOfFollow", "category"))
							.setResultTransformer(Transformers.aliasToBean(MostFollowingList.class));
					list = criteria.list();
					if (list.size() > 0) {
						return list;
					}

				} catch (HibernateException e) {
					System.out.println("Rolback: \n" + e.getMessage());
					e.printStackTrace();
				} finally {
					if (session != null) {
						session.close();
					}
				}
		return null;
	}
    
}
